package haoyou.csnf.manager.module.pm.controller.admin.employeeeducations;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import haoyou.csnf.manager.framework.common.pojo.PageParam;
import haoyou.csnf.manager.framework.common.pojo.PageResult;
import haoyou.csnf.manager.framework.common.pojo.CommonResult;
import haoyou.csnf.manager.framework.common.util.object.BeanUtils;
import static haoyou.csnf.manager.framework.common.pojo.CommonResult.success;

import haoyou.csnf.manager.framework.excel.core.util.ExcelUtils;

import haoyou.csnf.manager.framework.apilog.core.annotation.ApiAccessLog;
import static haoyou.csnf.manager.framework.apilog.core.enums.OperateTypeEnum.*;

import haoyou.csnf.manager.module.pm.controller.admin.employeeeducations.vo.*;
import haoyou.csnf.manager.module.pm.dal.dataobject.employeeeducations.EmployeeEducationsDO;
import haoyou.csnf.manager.module.pm.service.employeeeducations.EmployeeEducationsService;

@Tag(name = "管理后台 - 职工学历")
@RestController
@RequestMapping("/pm/employee-educations")
@Validated
public class EmployeeEducationsController {

    @Resource
    private EmployeeEducationsService employeeEducationsService;

    @PostMapping("/create")
    @Operation(summary = "创建职工学历")
    @PreAuthorize("@ss.hasPermission('pm:employee-educations:create')")
    public CommonResult<Long> createEmployeeEducations(@Valid @RequestBody EmployeeEducationsSaveReqVO createReqVO) {
        return success(employeeEducationsService.createEmployeeEducations(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新职工学历")
    @PreAuthorize("@ss.hasPermission('pm:employee-educations:update')")
    public CommonResult<Boolean> updateEmployeeEducations(@Valid @RequestBody EmployeeEducationsSaveReqVO updateReqVO) {
        employeeEducationsService.updateEmployeeEducations(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除职工学历")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pm:employee-educations:delete')")
    public CommonResult<Boolean> deleteEmployeeEducations(@RequestParam("id") Long id) {
        employeeEducationsService.deleteEmployeeEducations(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除职工学历")
                @PreAuthorize("@ss.hasPermission('pm:employee-educations:delete')")
    public CommonResult<Boolean> deleteEmployeeEducationsList(@RequestParam("ids") List<Long> ids) {
        employeeEducationsService.deleteEmployeeEducationsListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得职工学历")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pm:employee-educations:query')")
    public CommonResult<EmployeeEducationsRespVO> getEmployeeEducations(@RequestParam("id") Long id) {
        EmployeeEducationsDO employeeEducations = employeeEducationsService.getEmployeeEducations(id);
        return success(BeanUtils.toBean(employeeEducations, EmployeeEducationsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得职工学历分页")
    @PreAuthorize("@ss.hasPermission('pm:employee-educations:query')")
    public CommonResult<PageResult<EmployeeEducationsRespVO>> getEmployeeEducationsPage(@Valid EmployeeEducationsPageReqVO pageReqVO) {
        PageResult<EmployeeEducationsDO> pageResult = employeeEducationsService.getEmployeeEducationsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EmployeeEducationsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出职工学历 Excel")
    @PreAuthorize("@ss.hasPermission('pm:employee-educations:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEmployeeEducationsExcel(@Valid EmployeeEducationsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EmployeeEducationsDO> list = employeeEducationsService.getEmployeeEducationsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "职工学历.xls", "数据", EmployeeEducationsRespVO.class,
                        BeanUtils.toBean(list, EmployeeEducationsRespVO.class));
    }

}